package space.xxhui.forcelist;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;

import space.xxhui.forcelist.adapter.Adapter;
import space.xxhui.forcelist.impclass.RecycleItemTouchHelper;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private RecyclerView recyclerView;
    private List<String> data;
    private Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecycleView();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }


    private void initRecycleView() {
        initData();
        recyclerView = (RecyclerView) findViewById(R.id.rc_list);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(
                MainActivity.this, DividerItemDecoration.VERTICAL));
        adapter = new Adapter(data);

        ItemTouchHelper.Callback touchHelperCallback = new RecycleItemTouchHelper(adapter,MainActivity.this);

        ItemTouchHelper itemTouchHelper=new ItemTouchHelper(touchHelperCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);


        adapter.setOnItemClickListener(new Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(final View v, int position) {
                v.animate().rotationX(180f).setDuration(300).setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        v.animate().rotationX(-0f).setDuration(500).start();
                    }
                }).start();
                Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show();

            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        data = new ArrayList<String>();
        for (int i = 'A'; i < 'Z'; i++) {
            data.add((char) i + "");
        }
    }

    /**
     *添加两个菜单项
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //add (int groupId, int itemId, int order, CharSequence title)
        menu.add(0,2,0, "GridLayout");
        menu.add(0,1,1,"LinearLayout");
        return super.onCreateOptionsMenu(menu);
    }

    /**
     *根据itemId切换布局管理器
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                layoutManager = new LinearLayoutManager(this);
                recyclerView.setLayoutManager(layoutManager);
                break;
            case 2:
                layoutManager = new GridLayoutManager(MainActivity.this,3);
                recyclerView.setLayoutManager(layoutManager);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }



}
